
import { _decorator, Component, Node, Animation, SpriteFrame, AnimationClip, Size, UITransform, Vec2, Vec3 } from 'cc';
import { BulletOrigin } from './BulletOrigin';
import { ConfigKey, tasteMainGun_config } from '../../manager/AppControll';
import { MonsterOrigin } from '../monster/MonsterOrigin';
import { Tools } from '../../tools/Tools';
import { DataHelper } from '../../system/DataHelper';
import { SoundManager } from '../../manager/SoundManger';
import { resManger } from '../../tools/ResManger';
import { BulletManager } from './BulletManager';
const { ccclass, property } = _decorator;




@ccclass('BulletMissile')
export class BulletMissile extends BulletOrigin {
    private mTasteGunConfig: tasteMainGun_config = null;
    private mAnSpf: SpriteFrame[] = [];

    protected onLoad(): void {
        super.onLoad();
    }
    // dummy = '';
    InitConfig(config: tasteMainGun_config) {
        this.mTasteGunConfig = config;
        this.InitAnimation();
    }

    MoveFinish() {
        this.PlayAnimaition();
    }

    InitAnimation(callback?) {
        if (!this.mTasteGunConfig.aniRes) return;
        let an: Animation = this.node.getComponent(Animation);
        if (!an) an = this.node.addComponent(Animation);
        an.clips = [];
        resManger.GetSkillAniSpf(this.mTasteGunConfig.aniRes, (arr) => {
            this.mAnSpf = arr;
            const clip = AnimationClip.createWithSpriteFrames(this.mAnSpf, this.mAnSpf.length);
            clip.name = this.mTasteGunConfig.aniRes;
            clip.wrapMode = AnimationClip.WrapMode.Normal;
            clip.speed = this.mTasteGunConfig.skillAniSpeed;
            an.addClip(clip);
            if (callback) callback();
        })
    }

    PlayAnimaition() {
        if (this.mAnSpf.length > 0) {
            this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
                BulletManager.Instance<BulletManager>().missileBulletPool.put(this.node);
            });
            this.node.getComponent(UITransform).contentSize = new Size(this.mTasteGunConfig.skillRange[0], this.mTasteGunConfig.skillRange[1]);
            if (this.mTasteGunConfig.aniAudio != "") {
                SoundManager.Instance<SoundManager>().PlayEffect("gun/" + this.mTasteGunConfig.aniAudio, Number.parseFloat(DataHelper.GetConfigByKey(ConfigKey.gunVolume).value));
            }
            this.node.getComponent(Animation).play(this.mTasteGunConfig.aniRes)
        } else {
            BulletManager.Instance<BulletManager>().missileBulletPool.put(this.node);
        }
        this.GetBeingHit();
    }

    GetBeingHit() {
        let atk = DataHelper.GetAttack().attributeNum;
        let chtRate = DataHelper.GetCriticalHitRate().attributeNum;
        let chtdmg = DataHelper.GetCriticalHitDamage().attributeNum;
        let damageBoost = DataHelper.GetDamageBoost().attributeNum;
        let skinDmg = DataHelper.GetSkinBuffDmg();
        atk = atk + atk * (skinDmg - 1);
        let dmg = Tools.CalculateDamage(atk, chtRate, chtdmg, damageBoost, 0);
        let lt = Tools.GetLtMonster();

        lt.forEach((elemt) => {
            if (elemt.position.clone().subtract(this.node.position).length() <= this.mTasteGunConfig.skillRange[0]) {
                elemt.getComponent(MonsterOrigin).Attacked(dmg);
            }
        })
    }

    start() {
        // [3]
    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

